package com.changgou.oauth.config;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.changgou.oauth.dao.AdminMapper;
import com.changgou.oauth.dao.LoginMapper;
import com.changgou.oauth.util.UserJwt;
import com.lwy.goods.pojo.TbAdmin;
import com.lwy.goods.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/*****
 * 自定义授权认证类
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    ClientDetailsService clientDetailsService;

    @Resource
    LoginMapper loginMapper;

    @Resource
    AdminMapper adminMapper;

    /****
     * 自定义授权认证
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //取出身份，如果身份为空说明没有认证
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //没有认证统一采用httpbasic认证，httpbasic中存储了client_id和client_secret，开始认证client_id和client_secret
        if(authentication==null){
            ClientDetails clientDetails = clientDetailsService.loadClientByClientId(username);
            if(clientDetails!=null){
                //秘钥
                String clientSecret = clientDetails.getClientSecret();
                //静态方式
                return new User(username,new BCryptPasswordEncoder().encode(clientSecret), AuthorityUtils.commaSeparatedStringToAuthorityList(""));
                //数据库查找方式
                //return new User(username,clientSecret, AuthorityUtils.commaSeparatedStringToAuthorityList(""));
            }
        }

        if (StringUtils.isEmpty(username)) {
            return null;
        }

        //根据用户名称查询数据
        TbUser tbUser = loginMapper.selectOne(new QueryWrapper<TbUser>().eq("username", username));

        TbAdmin tbAdmin = adminMapper.selectOne(new QueryWrapper<TbAdmin>().eq("login_name", username));

        UserJwt userDetails =null;
        if(tbUser!=null){
            String name = "ROLE_USER";
            List<GrantedAuthority> list=AuthorityUtils.commaSeparatedStringToAuthorityList(name);
            userDetails = new UserJwt(username,tbUser.getPassword(),list);
        }else if(tbAdmin!=null){
            String name = "ROLE_ADMIN";
            List<GrantedAuthority> list=AuthorityUtils.commaSeparatedStringToAuthorityList(name);
            userDetails =new UserJwt(username,tbAdmin.getPassword(),list);
        }else{
            return null;
        }
        //userDetails.setComy(songsi);
        return userDetails;
    }
}
